<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "sys_source".
 *
 * @property int $id
 * @property string $name
 * @property string $url
 * @property int $parent
 * @property int $ismenu 是否是菜单
 * @property string $addtime
 */
class SysSource extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sys_source';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'addtime'], 'required'],
            [['parent', 'addtime'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['url'], 'string', 'max' => 130],
            [['ismenu'], 'string', 'max' => 1],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'url' => 'Url',
            'parent' => 'Parent',
            'ismenu' => '是否是菜单',
            'addtime' => 'Addtime',
        ];
    }
    
    //获取列表信息 带分页数据
    public static function getSourceList($condition,$page_no){
        //获取符合条件的商品的个数
        $perpage = 15;
        $sql_condition_arr = [];
        array_push($sql_condition_arr,'1=1');
        if($condition['name'] != ''){
            array_push($sql_condition_arr,"name='".$condition['name']."'");
        }
        
        $sql_condition_string = implode(' and ', $sql_condition_arr);
        $data['total'] = self::getCountByCondition($sql_condition_string);
        $begin = $perpage*($page_no-1);
        $sourcedata = self::getGoodsByPage($sql_condition_string, $begin, $perpage);
        $data['pagedata'] = $sourcedata;
        $data['pagenums'] = ceil($data['total']/$perpage);
        $data['page_no'] = $page_no;
        $data['perpage'] = $perpage;
        return $data;
    }
    
    public static function getCountByCondition($sql_condition_string){
        $sql = "select count(id) total from sys_source where $sql_condition_string";
        $command = \Yii::$app->db->createCommand($sql);
    	$model = $command->queryOne();
        return $model['total'];
    }
    
    //按页获取数据
    public static function getGoodsByPage($sql_condition_string,$begin,$perpage){
        $sql = "select id,name,url,parent,ismenu,addtime from sys_source where $sql_condition_string order by id desc limit $begin,$perpage";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
    
    public static function getSourceTree($role_id){
        //获取角色的所有权限
        $authes = SysRole2source::find()->where(['roleid'=>$role_id])->asArray()->all();
        
        $data = self::find()->where(['parent'=>0])->asArray()->all();
        foreach ($data as $key => $value){
            $has_power = 0;
            foreach($authes as $auth){
                if($auth['sourceid'] == $value['id']){
                    $has_power = 1;
                }
            }
            $data[$key]['has_power'] = $has_power;
            $data[$key]['sub_sources'] = self::getSubSources($value['id'],$authes);
        }
        return $data;
    }
    
    public static function getSubSources($parent,$authes){
        $sub_sources = self::find()->where(['parent'=>$parent])->asArray()->all();
        foreach ($sub_sources as $key=>$sub_source){
            $has_power = 0;
            foreach ($authes as $auth){
                if($auth['sourceid'] == $sub_source['id']){
                    $has_power = 1;
                    
                }
            }
            $sub_sources[$key]['has_power'] = $has_power;
        }
        return $sub_sources;
    }
    
    public static function getTopSources(){
        $sql = "select id,name from sys_source where parent=0 order by sort desc";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
}
